/*****************************************************************************
*    Copyright (C)2003 Ali Corporation. All Rights Reserved.
*
*    File:    nim_ix2360.c
*
*    Description:    This file contains ix2360 basic function in LLD for Sharp 0184 module.
*    History:
*           Date            Athor        Version          Reason
*	    ============	=============	=========	=================
*	1.  Jan.28.2004		David Wang		Ver 0.1		Create file.
*****************************************************************************/

#include <sys_config.h>
#include <retcode.h>
#include <types.h>

#include <api/libc/printf.h>
#include <bus/i2c/i2c.h>

#include "nim_ix2360.h"

struct QPSK_TUNER_CONFIG_EXT * stIX2360_dev_id[MAX_TUNER_SUPPORT_NUM] = {NULL};
static UINT32 stIX2360_tuner_cnt = 0;
INT32 nim_ix2360_init(UINT32 * tuner_id, struct QPSK_TUNER_CONFIG_EXT * ptrTuner_Config)
{
	struct QPSK_TUNER_CONFIG_EXT * stIX2360_ptr = NULL;
	if(NULL==ptrTuner_Config||stIX2360_tuner_cnt>=MAX_TUNER_SUPPORT_NUM)
		return ERR_FAILUE;
	stIX2360_ptr = (struct QPSK_TUNER_CONFIG_EXT *)MALLOC(sizeof(struct QPSK_TUNER_CONFIG_EXT));
	if(!stIX2360_ptr)
		return ERR_FAILUE;
	stIX2360_dev_id[stIX2360_tuner_cnt] = stIX2360_ptr;
	MEMCPY(stIX2360_ptr, ptrTuner_Config, sizeof(struct QPSK_TUNER_CONFIG_EXT));
	*tuner_id = stIX2360_tuner_cnt;
	stIX2360_tuner_cnt++;
	return RET_SUCCESS;
}
/*****************************************************************************
* INT32 nim_ix2360_control(UINT32 freq, UINT8 bs, UINT8 cp)
*
* Tuner write operation
*
* Arguments:
*  Parameter1: UINT32 freq		: Synthesiser programmable divider
*  Parameter2: UINT8 bs			: Baseband path select
*
* Return Value: INT32			: Result
*****************************************************************************/
INT32 nim_ix2360_control(UINT32 tuner_id, UINT32 freq, UINT32 sym)
{
	UINT8 data[4];
	UINT16 Npro,tmp;
	UINT8 Nref,Nswa;
	UINT8 bs;
	struct QPSK_TUNER_CONFIG_EXT * stIX2360_ptr = NULL;
	if(tuner_id>=stIX2360_tuner_cnt||tuner_id>=MAX_TUNER_SUPPORT_NUM)
		return ERR_FAILUE;
	stIX2360_ptr = stIX2360_dev_id[tuner_id];
	if(sym<12000)
		bs = 0;
	else
		bs = 1;
	Nref = 64;
	tmp = ((freq/2)*Nref)/4;
	Nswa = tmp%32;
	Npro = tmp/32;
	data[0] = (UINT8)((Npro >> 3)&0x7F);
	data[1] = Nswa|(((UINT8)Npro&0x07) << 5);
	data[2] = (UINT8)((Npro >> 5)&0x60)|0x88|0x06; /* PD4 == 1 */
	data[3] = 0x80;
	if(freq < 1450)
		data[3] = 0x82;
	if(bs == 1) /* More than 12Mbaud */
		data[3] |= 0x04;
	return i2c_write(stIX2360_ptr->i2c_type_id, stIX2360_ptr->cTuner_Base_Addr, data, 4);
}

/*****************************************************************************
* INT32 nim_ix2360_status(UINT8 *lock)
*
* Tuner read operation
*
* Arguments:
*  Parameter1: UINT8 *lock		: Phase lock status
*
* Return Value: INT32			: Result
*****************************************************************************/
INT32 nim_ix2360_status(UINT32 tuner_id, UINT8 *lock)
{
	INT32 result;
	UINT8 data;
	struct QPSK_TUNER_CONFIG_EXT * stIX2360_ptr = NULL;
	if(tuner_id>=stIX2360_tuner_cnt||tuner_id>=MAX_TUNER_SUPPORT_NUM)
	{
		*lock = 0;
		return ERR_FAILUE;
	}
	stIX2360_ptr = stIX2360_dev_id[tuner_id];
	result = i2c_read(stIX2360_ptr->i2c_type_id, stIX2360_ptr->cTuner_Base_Addr, &data, 1);
	*lock = ((data & 0x40) >> 6);
	return result;
}
